const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const cors = require("koa2-cors");
const koaBody = require("koa-body");
const staticFiles = require("koa-static");
const path = require("path");
const baseRequest = require("./middleware/baseRequest");
const baseController = require("./middleware/baseController");
const schedule = require("./middleware/schedule");
const sqlUpdate = require("./middleware/sqlUpdate");
const redis = require("./api/service/redis");
const db = require("./middleware/db");

const { port } = require("./config/config.json");

const app = new Koa();

// 配置静态web服务的中间件
app.use(staticFiles(path.resolve(__dirname, "./upload"), { extensions: ["html"] }));

// app.use(staticFiles(path.resolve(__dirname, "./admin/dist"), { extensions: ["html"] }));

app.use(
  cors({
    exposeHeaders: ["*"],
  })
);

let bodyConfig = {
  multipart: true,
  formidable: {
    maxFileSize: 200 * 1024 * 1024, // 设置上传文件大小最大限制，默认2M
  },
};
app.use(koaBody(bodyConfig));

// 格式化body
app.use(bodyParser());

// 过滤请求
app.use(baseRequest());

// 注册路由
app.use(baseController());

schedule.init();

// redis.init();

app.listen(port.node);

// sqlUpdate.init()

console.log(`http://localhost:${port.node}`);

// 同步数据库结构
//db.sync();

// 同步数据库结构
//db.sync();
